<script setup>
import { onMounted, ref, watchEffect, defineAsyncComponent, provide } from 'vue'
import { useRouter } from 'vue-router';
// import HistorySubmissionItem from '/src/components/HistorySubmissionItem.vue'
import getRequest from '../utils/lxw_request';

const props = defineProps(['isOpenModal'])
const emit = defineEmits(['update:isOpenModal'])

function closeModal() {
    emit('update:isOpenModal', false)
}




const router = useRouter();
const logs = ref([])
const course_id = router.currentRoute.value.params['subject_id']

const current = ref(1)
const pageSize = ref(10)

watchEffect(() => {
    getLogs();
})


function getLogs() {
    const searchParams = new URLSearchParams();
    searchParams.set("current", current.value);
    searchParams.set("pageSize", pageSize.value);

    getRequest(`/stu/course/${course_id}/logs?${searchParams.toString()}`, "GET", new Headers({ 'Content-Type': 'application/json' }))
        .then((response) => response.json())
        .then((json) => {
            if (json.code === 200) {
                json.data.records === undefined ? logs.value = [] : logs.value = json.data.records

            } else {
                alert(json.msg);
            }
        })
        .catch((err) => {
            console.error(err);
        })
}


const HistorySubmissionItem = defineAsyncComponent(() => import('../components/HistorySubmissionItem.vue'));
provide("getLogs", getLogs);

</script>
<template>
    <div class="history-submission-modal" :class="{ open: props.isOpenModal }">
        <div class="content">
            <div class="header">
                <img src="/src/assets/images/student/close.svg" alt="Delete" @click="closeModal" />
            </div>
            <div class="main">
                <HistorySubmissionItem v-for="(log, index) in logs" style="margin-bottom: 15px" :key="index" :log="log" />
                <div v-if="logs.length <= 0">暂无日志</div>
            </div>
        </div>
    </div>
</template>
<style scoped>
.history-submission-modal {
    display: none;
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.5);
    overflow: auto;
    z-index: 15;
}

.history-submission-modal.open {
    display: flex;
    justify-content: center;
    align-items: start;
}

.history-submission-modal>.content {
    width: 800px;
    background-color: #ffffff;
    border: none;
    border-radius: 10px;
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
    padding: 20px 50px;
    margin-top: 65px;
}

.history-submission-modal>.content>.header {
    display: flex;
    justify-content: end;
    align-items: center;
    margin-bottom: 30px;
}


.content>.header>img {
    cursor: pointer;
    user-select: none;
    transition: transform 0.3s;
}

.content>.header>img:hover {
    transform: rotate(90deg);
}

.content>.main {
    margin-bottom: 20px;
}


 
</style>